package cn.zhoujing.security.demo.common.handler;

import cn.zhoujing.security.demo.common.api.CommonResult;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import java.io.IOException;

/**
 * 权限不足异常处理器
 *
 * @author zhoujing
 * @createTime 2024/2/8 - 1:44
 */
@Slf4j
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        System.err.println("[MyAccessDeniedHandler]："+accessDeniedException.toString());
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().print(JSON.toJSONString(CommonResult.forbidden(null)));
        response.getWriter().flush();
    }

}
